package com.tx.component.file.starter;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import com.tx.component.file.FileContextConstants;
import com.tx.component.file.context.FileEngineFactory;
import com.tx.component.file.controller.FileContextController;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.starter.component.ComponentSupportAutoConfiguration;

/**
 * 文件容器<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2020年2月18日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Configuration
@AutoConfigureAfter({ ComponentSupportAutoConfiguration.class })
@EnableConfigurationProperties(FileContextProperties.class)
@ConditionalOnClass({ FileEngineFactory.class })
@ConditionalOnSingleCandidate(DataSource.class)
@ConditionalOnBean(PlatformTransactionManager.class)
@ConditionalOnProperty(prefix = FileContextConstants.PROPERTIES_PREFIX, value = "enable", havingValue = "true")
public class FileContextAutoConfiguration
        implements InitializingBean, ApplicationContextAware {
    
    /** spring容器句柄　*/
    @SuppressWarnings("unused")
    private ApplicationContext applicationContext;
    
    /** 基础包 */
    @SuppressWarnings("unused")
    private String basePackages;
    
    /** 事务管理器 */
    @SuppressWarnings("unused")
    private PlatformTransactionManager transactionManager;
    
    /** 属性文件 */
    private FileContextProperties properties;
    
    /** application.name */
    @Value(value = "${spring.application.name}")
    private String applicationName;
    
    /** 容器所属模块：当该值为空时，使用spring.application.name的内容 */
    private String module;
    
    /** <默认构造函数> */
    public FileContextAutoConfiguration(FileContextProperties properties,
            PlatformTransactionManager transactionManager) {
        super();
        this.properties = properties;
        this.transactionManager = transactionManager;
    }
    
    /**
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;
    }
    
    /**
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //初始化包名
        if (!StringUtils.isBlank(this.applicationName)) {
            this.module = this.applicationName;
        }
        if (!StringUtils.isEmpty(this.properties.getModule())) {
            this.module = this.properties.getModule();
        }
        AssertUtils.notEmpty(this.module, "module is empty.");
    }
    
    /**
     * 注册文件容器<br/>
     * <功能详细描述>
     *
     * @return FileContextFactroy [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @Bean(name = { "fileEngine", "fileContext" })
    public FileEngineFactory fileEngine() {
        FileEngineFactory fileContext = new FileEngineFactory();
        return fileContext;
    }
    
    /**
     * 控制层自动配置层<br/>
     * <功能详细描述>
     * 
     * @author  Administrator
     * @version  [版本号, 2020年3月19日]
     * @see  [相关类/方法]
     * @since  [产品/模块版本]
     */
    @Configuration
    @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class })
    public class ControllerAutoConfiguration {
        
        /**
         * 文件容器控制层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return FileContextController [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(name = "fileContextController")
        public FileContextController fileContextController() {
            FileContextController controller = new FileContextController();
            return controller;
        }
    }
}
